
<?php
@$act = $_GET['act'];
@$id = $_GET['id'];
@$loai_tin = $_POST['selLoai'];
@$tieu_de = $_POST['txtTitle'];
@$tieu_de_en = $_POST['txtTitle_en'];
//@$path_hinh = $_POST['txtPic'];
@$name_hinh = $_FILES['txtPic']['name'];
@$path_hinh = $_FILES['txtPic']['tmp_name'];
@$tt_nd = $_POST['txtDesc'];
@$tt_nd_en = $_POST['txtDesc_en'];
@$cont = $_POST['txtCont'];
@$cont_en = $_POST['txtCont_en'];
$cur_date = date('Y-m-d');

if(isset($_POST['btnSave']))
{
	@$act_p = $_POST['txtEdit'];
	@$id_p = $_POST['txtIdEdit'];
	$hinh = $exec_mysql->execute("select img from node where id = $id_p",0);
	@$row = mysql_fetch_array($hinh);
	@$path_hinh_u = $row['img'];
	//echo 'here!';
	switch($loai_tin)
	{
		case 'gioi_thieu';
			$id_type = 2;
			$id_ref = 1;
		break;
		case 'dich_vu';
			$id_type = 1;
			$id_ref = 2;
		break;
		case 'san_pham';
			$id_type = 1;
			$id_ref = 3;
		break;
		case 'du_an';
			$id_type = 1;
			$id_ref = 4;
		break;
		default;
			$id_type = '';
			$id_ref = '';
	}
	if($name_hinh != "")
	{
		$uploaddir = 'img/'; 
    	$ext = explode('.',$name_hinh);
		if($ext[1]=='gif' || $ext[1]=='GIF'|| $ext[1]=='jpg' || $ext[1]=='JPG' || $ext[1]=='png' || $ext[1]=='PNG')
		{
			$id=rand();
        	$filename=$id.'.'.$ext[1];
        	$uploadfile = $uploaddir . $filename;
			$size=round(filesize($path_hinh)/1000,2);
			if($size < 3000)
			{
				if (move_uploaded_file($path_hinh, $uploadfile))
				{
					echo '<i>(Dung lượng file ảnh đính kèm: '.$size.' Kb)</i>';
                	@$path_hinh_u = $uploadfile;
				}
				else
				{
					echo "Có lỗi xảy ra khi upload ảnh!"; 
				}
			}
			else
			{
				echo "Không thể upload được ảnh. File ảnh upload phải nhỏ hơn 3Mb!";
			}
		}
		else
		{
			echo "Không thể upload được ảnh. File ảnh không đúng chuẩn (jpg, jpeg, gif, png)!";
		}
	}
	
	$val = $exec_mysql->escape_string(array($id_type, $id_ref, $path_hinh_u, $tieu_de, $tieu_de_en, $tt_nd, $tt_nd_en, $cont, $cont_en, $cur_date));
	
	if(@$act_p == 'edit')
	{
		if($row['img'] != $path_hinh_u && $path_hinh_u != "" && $row['img'] !="")
		{
			unlink($row['img']);
		}
		$saved = $exec_mysql->execute("update node set id_type = '$val[0]', id_ref = '$val[1]', img = '$val[2]', title_vi = '$val[3]', title_en = '$val[4]', description_vi = '$val[5]', description_en = '$val[6]', content_vi = '$val[7]', content_en = '$val[8]' where id = $id_p", 0);
	}
	else 
	{
		$saved = $exec_mysql->execute("insert into node values ('','$val[0]','$val[1]','$val[2]','$val[3]','$val[4]','$val[5]','$val[6]','$val[7]','$val[8]','$val[9]')",0);
	}
	if($saved == 1)
	{
		echo "<script>showAlert('Thông báo','Lưu dữ liệu thành công!','');</script>";
	}
	else
	{
		echo "<script>showAlert('Lỗi','Chưa thể lưu dữ liệu. Vui lòng kiểm tra lại!','');</script>";
	}
}

if(@$act == 'del')
{
	//echo 'xoa recode';
	$hinh = $exec_mysql->execute("select img from node where id = $id",0);
	$row = mysql_fetch_array($hinh);
	unlink($row['img']);
	$xoa = $exec_mysql->execute("delete from node where id = $id",0);
	echo "<script>";
		echo "window.location = 'admin_master_page.php?cmd=1'";
	echo "</script>";
}

if(@$act == 'edit')
{
	$getdata = $exec_mysql->execute("select * from node where id = $id",0);
	$row = mysql_fetch_array($getdata);
	if($row['id_ref'] == '2')
	{
		$dv = 'selected="selected"';
		//echo 'Loai tin:'.$dv;
	}
	if($row['id_ref'] == '3')
	{
		$sp = 'selected="selected"';
	}
	if($row['id_ref'] == '4')
	{
		$da = 'selected="selected"';
	}
	$ed = 'edit';
}

$result = $exec_mysql->execute("select * from node where id_type = 1 or id_type = 2 order by id_type, id desc",0);
?>


<script type="text/javascript">
function checkSubmit(){
	if($('#txtTitle').val() == ''){
		showAlert('Lỗi', 'Vui lòng nhập tiêu đề!', 'txtTitle');
		return false;
	}
	if($('#txtDesc').val() == ''){
		showAlert('Lỗi', 'Vui lòng nhập tóm tắt nội dung!', 'txtDesc');
		return false;
	}
	
}
function deleletconfig(){

    var del=confirm("Bạn muốn xóa mẩu tin?");
    
    return del;
    }
</script>
<link rel="stylesheet" href="css/text_css_hqk.css" type="text/css" />
<p class="group-line">
   <span class="group-title">Thêm và cập nhật thông tin</span>
</p>
<div id="frmAdd">
<form name="frmAddNode" action="admin_master_page.php?cmd=1" method="post" enctype="multipart/form-data" onsubmit="return checkSubmit()">
    <table class="form-layout" width="650px" border="0px">
        <tr>
            <td width="150px">Chọn loại tin</td>
            <td>
                <select name="selLoai" id="selLoai">
                    <option value="gioi_thieu">Giới thiệu</option>
                    <option <?php echo @$dv; ?> value="dich_vu">Dịch vụ</option>
                    <option <?php echo @$sp; ?> value="san_pham">Sản phẩm</option>
                    <option <?php echo @$da; ?> value="du_an">Dự án</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Tiêu đề / Title</td>
            <td>
            	<input type="text" name="txtTitle" id="txtTitle" style="width:485px; margin-bottom:3px" value="<?php echo @$row['title_vi']; ?>" />
                <input type="text" name="txtTitle_en" id="txtTitle_en" style="width:485px" value="<?php echo @$row['title_en']; ?>"/>
            </td>
        </tr>
        <tr>
            <td>Hình minh họa</td>
            <td>
            	<input type="file" name="txtPic" id="txtPic" /> 
                <?php
				if(isset($row['img']) && isset($row['title_vi']))
				{
					echo '<img src="'.$row['img'].'" width="80px">';
				}
			?>
            </td>
        </tr>
        <tr>
            <td>Tóm tắt nội dung / Description</td>
            <td>
            	<textarea name="txtDesc" id="txtDesc" style="width:485px; margin-bottom:3px" rows="3"><?php echo @$row['description_vi']; ?></textarea>
                <textarea name="txtDesc_en" id="txtDesc_en" style="width:485px" rows="4"><?php echo @$row['description_en']; ?></textarea>
            </td>
        </tr>
        <tr>
            <td colspan="2">
            	<textarea name="txtCont" id="txtCont" rows="4"><?php echo @$row['content_vi']; ?></textarea>
                <textarea name="txtCont_en" id="txtCont_en" rows="4"><?php echo @$row['content_en']; ?></textarea>
            </td>
            <script>
				CKEDITOR.replace( 'txtCont',{
					enterMode: CKEDITOR.ENTER_BR
				});
				CKEDITOR.replace( 'txtCont_en',{
					enterMode: CKEDITOR.ENTER_BR
				});
            </script>
        </tr>
    </table>
    <p class="footer-form">
    	<input type="hidden" name="txtEdit" value="<?php echo @$ed; ?>" />
        <input type="hidden" name="txtIdEdit" value="<?php echo @$id; ?>" />
    	<input type="submit" name="btnSave" value="Lưu" />
        <input type="reset" name="btnDelete" value="Xóa" />
    </p>
</form>
</div>


<p class="group-line">
   <span class="group-title">Thông tin trang chủ đã lưu</span>
</p>

<table width="680px" class="table-list">
	<tr class="table-header">
        <td width="20px">STT</td>
        <td width="200px">Tiêu đề</td>
        <td>Tóm tắt nội dung</td>
        <td width="45px">Cập nhật</td>
        <td width="45px">Xóa</td>
    </tr>
    <?php
	$p = 1;
	while($row = mysql_fetch_array($result))
	{
		echo '<tr>';
			echo '<td align="center">'.$p++.'</td>';
			echo '<td>'.$row['title_vi'].' / '.$row['title_en'].'</td>';
			echo '<td>'.cut($row['description_vi'],50).' / '.cut($row['description_en'],50).'</td>';
			echo '<td align="center"><a href="admin_master_page.php?cmd=1&id='.$row['id'].'&act=edit"><img src="img/ico_edit.png" /></a></td>';
			echo '<td align="center"><img src="img/ico_delete.png" onclick="showConfirm(\'Xác nhận\', \'Bạn có chắc muốn xóa không?\', \'admin_master_page.php?cmd=1&act=del&id='.$row['id'].'\')" class="del" /></td>';
		echo '</tr>';
	}
	?>
    
</table>
<?php
function cut($str, $len) {
    $str = trim($str);
    if (strlen($str) <= $len) return $str;
    $str = substr($str, 0, $len);
    if ($str != "") {
        if (!substr_count($str, " ")) return $str." ...";
        while (strlen($str) && ($str[strlen($str) - 1] != " ")) $str = substr($str, 0, -1);
        $str = substr($str, 0, -1)." ...";
    }
    return $str;
}	
?>
